package cn.edu.qzuie.artist.module.td.controller.app.work;

import cn.edu.qzuie.artist.module.td.controller.admin.work.vo.WorkRespVO;
import cn.edu.qzuie.artist.module.td.controller.admin.work.vo.WorkSaveReqVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.edu.qzuie.artist.framework.common.pojo.PageParam;
import cn.edu.qzuie.artist.framework.common.pojo.PageResult;
import cn.edu.qzuie.artist.framework.common.pojo.CommonResult;
import cn.edu.qzuie.artist.framework.common.util.object.BeanUtils;
import static cn.edu.qzuie.artist.framework.common.pojo.CommonResult.success;

import cn.edu.qzuie.artist.framework.excel.core.util.ExcelUtils;

import cn.edu.qzuie.artist.framework.apilog.core.annotation.ApiAccessLog;
import static cn.edu.qzuie.artist.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.edu.qzuie.artist.module.td.controller.app.work.vo.*;
import cn.edu.qzuie.artist.module.td.dal.dataobject.work.WorkDO;
import cn.edu.qzuie.artist.module.td.service.work.WorkService;

@Tag(name = "用户 APP - TD作品")
@RestController
@RequestMapping("/td/work")
@Validated
public class AppWorkController {

    @Resource
    private WorkService workService;

    @PostMapping("/create")
    @Operation(summary = "创建作品")
    public CommonResult<Boolean> createWork(@Valid @RequestBody AppWorkSaveReqVO appCreateReqVO) {

        return success(workService.save(BeanUtils.toBean(appCreateReqVO,WorkDO.class)));
    }

//    @PutMapping("/update")
//    @Operation(summary = "更新作品")
//    public CommonResult<Boolean> updateWork(@Valid @RequestBody AppWorkSaveReqVO updateReqVO) {
//        workService.updateWork(updateReqVO);
//        return success(true);
//    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除作品")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteWork(@RequestParam("id") Long id) {
        workService.deleteWork(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得作品")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<AppWorkRespVO> getWork(@RequestParam("id") Long id) {
        WorkRespVO work = workService.getWork(id);
        return success(BeanUtils.toBean(work, AppWorkRespVO.class));
    }

    @GetMapping("/getWorksByUserId")
    @Operation(summary = "获得用户作品")
    @Parameter(name = "userId", description = "用户编号", required = true, example = "1")
    public CommonResult<List<AppWorkRespVO>> getWorksByUserId(@RequestParam("userId") Long userId) {
        QueryWrapper<WorkDO> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<WorkDO> works = workService.list(queryWrapper);
        return success(BeanUtils.toBean(works, AppWorkRespVO.class));
    }

//    @GetMapping("/page")
//    @Operation(summary = "获得作品分页")
//    public CommonResult<PageResult<AppWorkRespVO>> getWorkPage(@Valid AppWorkPageReqVO pageReqVO) {
//        PageResult<WorkDO> pageResult = workService.getWorkPage(pageReqVO);
//        return success(BeanUtils.toBean(pageResult, AppWorkRespVO.class));
//    }

//    @GetMapping("/export-excel")
//    @Operation(summary = "导出作品 Excel")
//    @ApiAccessLog(operateType = EXPORT)
//    public void exportWorkExcel(@Valid AppWorkPageReqVO pageReqVO,
//              HttpServletResponse response) throws IOException {
//        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
//        List<WorkDO> list = workService.getWorkPage(pageReqVO).getList();
//        // 导出 Excel
//        ExcelUtils.write(response, "作品.xls", "数据", AppWorkRespVO.class,
//                        BeanUtils.toBean(list, AppWorkRespVO.class));
//    }

}